home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / inventor / SpaceballViewer / SceneViewer.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.7 KB  |  177 lines

  1. /*
  2.  * Copyright (c) 1991-94 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that the name of Silicon Graphics may not be used in any advertising or
  7.  * publicity relating to the software without the specific, prior written
  8.  * permission of Silicon Graphics.
  9.  *
  10.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  11.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  12.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  13.  *
  14.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  15.  * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  16.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
  17.  * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
  18.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19.  */
  20. //
  21. //  Sample SceneViewer program.
  22. //   This program reads in a Inventor datafile, and creates a SceneViewer
  23. //   to allow interaction with it.
  24. //
  25.  
  26. #include <X11/Intrinsic.h>
  27. #include <X11/keysym.h>
  28.  
  29. #include <Inventor/SoDB.h>
  30. #include <Inventor/SoInteraction.h>
  31. #include <Inventor/SoPickedPoint.h>
  32. #include <Inventor/Xt/SoXt.h>
  33. #include <Inventor/nodekits/SoBaseKit.h>
  34. #include <Inventor/nodekits/SoNodeKit.h>
  35. #include <Inventor/nodes/SoSelection.h>
  36.  
  37. #include "SoSceneViewer.h"
  38.  
  39. // #define TEST_PICK_CB
  40. // #define DISABLE_SEL_PICK_MATCH
  41.  
  42. #ifdef TEST_PICK_CB
  43. // Test the selection pick callback mechanism
  44. static SoPath *
  45. selectionPickCB(void *userData, const SoPickedPoint *pick)
  46. {
  47.     // Pick the topmost group beneath the selection node
  48.     SoPath *p = pick->getPath();
  49.     SoSelection *sel = (SoSelection *) userData;
  50.     
  51.     // See which child of selection got picked
  52.     int selIndex = -1;
  53.     if (p->getHead() == sel)
  54.     selIndex = 0;
  55.     else {  
  56.     for (int i = 1; (selIndex == -1) && (i < p->getLength() - 1); i++)
  57.         if (p->getNode(i) == sel)
  58.         selIndex = i;
  59.     }
  60.     
  61.     // Return a path truncated to this point
  62.     if (selIndex == -1) {
  63.     fprintf(stderr, "SceneViewer selectionPickCB - did not find sel node in pick path!\n");
  64.     return NULL;
  65.     }
  66.     
  67.     return p->copy(selIndex, 2); // 2 nodes, starting at selection node
  68. }
  69. #endif
  70.  
  71. static SbBool
  72. getArgs(int argc, char **argv, char *&envFile, char *&filename)
  73. {
  74.     SbBool ok = TRUE;
  75.     
  76.     envFile = NULL;
  77.     filename = NULL;
  78.  
  79.     if (argc == 2) {
  80.     filename = argv[1];
  81.     }
  82.     else if (argc > 2) {
  83.         // see if there is a -e before the file name
  84.         if (strcmp(argv[1], "-e") == 0) {
  85.         envFile = argv[2];
  86.         if (argc > 3)
  87.             filename = argv[3];
  88.     }
  89.     else if (argc == 4) {
  90.         // maybe the -e is after the file name
  91.         if (strcmp(argv[2], "-e") == 0) {
  92.         envFile = argv[3];
  93.         filename = argv[1];
  94.         }
  95.     }
  96.     else ok = FALSE;
  97.     }
  98.     
  99.     return ok;
  100. }
  101.  
  102. void main(int argc, char **argv)
  103. {
  104.     Widget         mainWindow;
  105.     SoSceneViewer    *sv;
  106.     SoInput        in;
  107.     char        *envFile, *filename;
  108.  
  109.     // check usage
  110.     if (! getArgs(argc, argv, envFile, filename)) {
  111.     fprintf(stderr, "usage:  SceneViewer [-e environ.iv] [file.iv]\n");
  112.     exit( 1 );
  113.     }
  114.     
  115.     // workaround for bug 200909 - this will force OpenGL to create
  116.     // a connection with the X server to receive delete window events
  117.     // (so that OpenGL can delete Accumulation and other software
  118.     // buffers when the window is destroyed).
  119.     if (putenv("GL_CHECK_WINDOW_DESTROY=y"))
  120.     fprintf(stderr, "Could not set the GL_CHECK_WINDOW_DESTROY env.\n");
  121.     
  122.     SbBool useStdin = (filename && strcmp(filename,"-")==0);
  123.  
  124.     // init Inventor
  125.     mainWindow = SoXt::init(argv[0]);
  126.  
  127.     // read the file in, creating a selection node as the root
  128.     // of the scene graph.
  129.     SoSelection *selRoot = new SoSelection;
  130.     selRoot->ref();
  131. #ifdef TEST_PICK_CB
  132.     selRoot->setPickFilterCallback(selectionPickCB, selRoot);
  133. #endif
  134.  
  135. #ifdef DISABLE_SEL_PICK_MATCH
  136.     selRoot->setPickMatching(FALSE);
  137. #endif
  138.     
  139.     SbBool doRead = FALSE;
  140.     if (useStdin) {
  141.     doRead = TRUE;
  142.     in.setFilePointer(stdin);
  143.     }
  144.     else if (filename) {
  145.     doRead = in.openFile(filename);
  146.     }
  147.     
  148.     if (doRead) {
  149.     fprintf( stderr, "Reading input file...");
  150.     SoNode *n;
  151.     while ((SoDB::read(&in, n) != FALSE) && (n != NULL))
  152.         selRoot->addChild(n);
  153.     
  154.     if ( selRoot->getNumChildren() == 0 )
  155.         fprintf(stderr, "No data read; creating empty scene.\n" );
  156.     else
  157.         fprintf(stderr, "done.\n");
  158.     }
  159.  
  160.     //
  161.     // Create the SceneViewer
  162.     //
  163.     sv = new SoSceneViewer(mainWindow, NULL, TRUE, selRoot, envFile);
  164.     selRoot->unref();
  165.  
  166.     //
  167.     // Build and show the SceneViewer
  168.     //
  169.     sv->show();
  170.     XtRealizeWidget(mainWindow);
  171.  
  172.     //
  173.     // Loop forever
  174.     //
  175.     SoXt::mainLoop();
  176. }
  177.